package com.apexsoft.front.common.db.mysql;

import java.lang.reflect.Field;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import com.apexsoft.front.common.utils.LogUtil;

public class BaseJdbcTemplate extends JdbcTemplate {
	private static Map<String, List<TableFiled>> tableConf = new HashMap<String, List<TableFiled>>();

	public BaseJdbcTemplate(DataSource dataSource) {
		super(dataSource);
	}

	/**
	 * @Title: querySqlByPage
	 * @Description: 分页查询
	 * @param sql
	 * @param params
	 * @param pagenum
	 * @param pagesize
	 * @param clazz
	 * @return
	 */
	public List<?> querySqlByPage(String sql, Object[] params, int pagenum, int pagesize, Class<? extends ReflectObject> clazz) {
		int rowCount = querySqlCount(sql, params);
		LogUtil.infoLog(BaseJdbcTemplate.class, "SQL:" + sql);
		LogUtil.infoLog(BaseJdbcTemplate.class, params);
		if (rowCount == 0)
			return Collections.EMPTY_LIST;
		int pageCount = rowCount % pagesize == 0 ? rowCount / pagesize : rowCount / pagesize + 1;
		if (pagenum > pageCount)
			pagenum = pageCount;
		if (pagenum < 1)
			pagenum = 1;
		int startNum = pagesize * (pagenum - 1);
		sql = getPageSQL(sql, startNum, pagesize);
		return super.query(sql, params, new DaoSupportMapper(clazz));
	}

	/**
	 * @Title: querySql
	 * @Description: 全查询
	 * @param sql
	 * @param params
	 * @param clazz
	 * @return
	 */
	public List<?> querySql(String sql, Object[] params, Class<? extends ReflectObject> clazz) {
		return super.query(sql, params, new DaoSupportMapper(clazz));
	}

	/**
	 * @Title: querySqlCount
	 * @Description: 查询总数
	 * @param sql
	 * @param params
	 * @return
	 */
	public int querySqlCount(String sql, Object[] params) {
		StringBuffer countSql = new StringBuffer("select count(1) count from (").append(sql).append(") t");
		return ((Count) super.queryForObject(countSql.toString(), params, new DaoSupportMapper(Count.class))).count.intValue();
	}

	public static class Count extends ReflectObject {
		/**
		 * @Fields serialVersionUID : TODO
		 */
		private static final long serialVersionUID = 1L;
		Long count;
	}

	private String getPageSQL(String sql, int startNum, int pagesize) {
		StringBuffer pageSql = new StringBuffer();
		pageSql.append(sql);
		pageSql.append(" limit " + startNum + "," + pagesize);
		return pageSql.toString();
	}

	/**
	 * @Title: insertObject
	 * @Description: 插入数据， 如果有ID字段，不过插入该ID字段，而是使用MYSQL自增长ID
	 * @param pojo
	 * @return 失败返回-1， 成功返回插入的表的ID
	 */
	public long insertObject(ReflectObject pojo) {
		long ret = -1;
		List<Object> params = new ArrayList<Object>();
		StringBuffer insertSql = new StringBuffer();
		if (this.initInsertConfig(pojo, insertSql, params)) {
			final String sqlRst = insertSql.toString();
			final List<Object> paramsRst = params;
			KeyHolder keyHolder = new GeneratedKeyHolder();
			try {
				super.update(new PreparedStatementCreator() {
					public PreparedStatement createPreparedStatement(java.sql.Connection con) throws SQLException {
						// TODO Auto-generated method stub
						PreparedStatement ps = con.prepareStatement(sqlRst, new String[] {"ID"});
					  	for (int i = 0; i < paramsRst.size(); i++) {
					  		ps.setObject(i+1, paramsRst.get(i));
					  	}
			            return ps;
					}
			    }, keyHolder);
				ret = keyHolder.getKey().longValue();
			} catch (DataAccessException e) {
				LogUtil.infoLog(e.getMessage());
				ret = -1;
			} catch (IllegalArgumentException e) {
				LogUtil.infoLog(e.getMessage());
				ret = -1;
			}
		}
		LogUtil.infoLog(BaseJdbcTemplate.class, "SQL:" + insertSql.toString());
		LogUtil.infoLog(BaseJdbcTemplate.class, params);
		return ret;
	}

	/**
	 * @Title: updateObject
	 * @Description: 更新数据，如果有ID字段且ID不为主键，会忽略ID字段的拼接。
	 * @param pojo
	 * @return 失败返回-1
	 */
	public int updateObject(ReflectObject pojo) {
		int ret = -1;
		List<Object> params = new ArrayList<Object>();
		StringBuffer updateSql = new StringBuffer();
		if (this.initUpdateConfig(pojo, updateSql, params)) {
			try {
				ret = super.update(updateSql.toString(), params.toArray());
			} catch (DataAccessException e) {
				LogUtil.infoLog(e.getMessage());
				ret = -1;
			} catch (IllegalArgumentException e) {
				LogUtil.infoLog(e.getMessage());
				ret = -1;
			}
		}
		LogUtil.infoLog(BaseJdbcTemplate.class, "SQL:" + updateSql.toString());
		LogUtil.infoLog(BaseJdbcTemplate.class, params);
		return ret;
	}

	/**
	 * @Title: getObject
	 * @Description: 获取数据
	 * @param pojo
	 * @param id
	 * @return 查不到数据返回null
	 */
	public ReflectObject getObjectById(ReflectObject pojo, Object id) {
		try {
			List<?> list = super.query("select * from " + pojo.getTableName() + " where " + "ID" + "=?", new Object[] { id }, new DaoSupportMapper(pojo.getClass()));
			if (list.size() > 0) {
				return (ReflectObject) list.get(0);
			}
		} catch (DataAccessException e) { 
			LogUtil.infoLog(e.getMessage());
		} catch (IllegalArgumentException e) {
			LogUtil.infoLog(e.getMessage());
		}
		return null;
	}
	
	/**
	 * 通过ReflectObject中的主键获取对象
	 * @param pojo
	 * @return null：查询失败
	 */
	public ReflectObject getObjectByPK(ReflectObject pojo) {
		List<Object> params = new ArrayList<Object>();
		StringBuffer selectSql = new StringBuffer();
		if (this.initSelectConfigByPK(pojo, selectSql, params)) {
			try {
				List<?> list = super.query(selectSql.toString(), params.toArray(), new DaoSupportMapper(pojo.getClass()));
				if (list.size() > 0)
					return (ReflectObject) list.get(0);
			} catch (DataAccessException e) {
				LogUtil.infoLog(e.getMessage());
			} catch (IllegalArgumentException e) {
				LogUtil.infoLog(e.getMessage());
			}
		}
		return null;
	}
	
	/**
	 * 获取所有的满足条件的对象，如果有ID字段，查询条件会自动忽略ID字段的值
	 * @param pojo
	 * @return null：查询失败
	 */
	public List<?> getAllObject(ReflectObject pojo) {
		List<Object> params = new ArrayList<Object>();
		StringBuffer selectSql = new StringBuffer();
		if (this.initSelectConfigWithAnd(pojo, selectSql, params)) {
			try {
				List<?> list = super.query(selectSql.toString(), params.toArray(), new DaoSupportMapper(pojo.getClass()));
				return list;
			} catch (DataAccessException e) {
				LogUtil.infoLog(e.getMessage());
			}  catch (IllegalArgumentException e) {
				LogUtil.infoLog(e.getMessage());
			}
		}
		return null;
	}

	/**
	 * @Title: existObject
	 * @Description: 通过ID，查找表记录是否存在
	 * @param pojo
	 * @param id
	 * @return
	 */
	public boolean existObject(ReflectObject pojo, Object id) {
		boolean ret = false;
		Object[] params = new Object[] { id };
		try {
			Count count = (Count) super.queryForObject("select count(1) count from " + pojo.getTableName() + " where " + "ID" + "=?", params, new DaoSupportMapper(Count.class));
			LogUtil.infoLog(BaseJdbcTemplate.class, params);
			if (count.count.intValue() > 0)
				ret = true;
		} catch (DataAccessException e) {
			LogUtil.infoLog(e.getMessage());
		} catch (IllegalArgumentException e) {
			LogUtil.infoLog(e.getMessage());
		}
		return ret;
	}
	
	/**
	 * 通过ReflectObject中的primary key，查询是否存在数据
	 * @param pojo
	 * @return
	 */
	public boolean existObject(ReflectObject pojo) {
		boolean ret = false;
		List<Object> params = new ArrayList<Object>();
		StringBuffer selectCountSql = new StringBuffer();
		if (this.initSelectCountConfigByPK(pojo, selectCountSql, params)) {
			try {
				Count count = (Count) super.queryForObject(selectCountSql.toString(), params.toArray(), new DaoSupportMapper(Count.class));
				if (count.count.intValue() > 0)
					ret = true;
			} catch (DataAccessException e) {
				LogUtil.infoLog(e.getMessage());
			}  catch (IllegalArgumentException e) {
				LogUtil.infoLog(e.getMessage());
			}
		}
		return ret;
	}

	/**
	 * @Title: deleteObject
	 * @Description: 通过表ID删除数据
	 * @param pojo
	 * @param id
	 * @return
	 */
	public boolean deleteObjectById(ReflectObject pojo, Object id) {
		boolean ret = false;
		StringBuffer deleteSql = new StringBuffer();
		deleteSql.append("delete from ");
		deleteSql.append(pojo.getTableName());
		deleteSql.append(" where ");
		deleteSql.append("ID");
		deleteSql.append(" =?");
		try {
			int count = this.update(deleteSql.toString(), new Object[] { id });
			if (count > 0)
				ret = true;
		} catch (DataAccessException e) {
			LogUtil.infoLog(e.getMessage());
		}  catch (IllegalArgumentException e) {
			LogUtil.infoLog(e.getMessage());
		}
		LogUtil.infoLog(BaseJdbcTemplate.class, "SQL:" + deleteSql.toString());
		LogUtil.infoLog(BaseJdbcTemplate.class, id.toString());
		return ret;
	}
	
	/**
	 * 通过ReflectObject的primary key删除数据
	 * @param pojo
	 * @return 成功返回true
	 */
	public boolean deleteObjectByPK(ReflectObject pojo) {
		boolean ret = false;
		List<Object> params = new ArrayList<Object>();
		StringBuffer deleteSql = new StringBuffer();
		if (this.initDeleteConfigByPK(pojo, deleteSql, params)) {
			try {
				int count = this.update(deleteSql.toString(), params.toArray());
				if (count > 0)
					ret = true;
			} catch (DataAccessException e) {
				LogUtil.infoLog(e.getMessage());
			} catch (IllegalArgumentException e) {
				LogUtil.infoLog(e.getMessage());
			}
		}
		return ret;
	}
	
	/**
	  * 初始化delete 的sql语句， 对pojo中的primary key值进行拼接。如果primary key是ID，也支持拼接
	 * 注意：pojo中的属性应该对象， 不能是int等基本类型
	 * @param pojo
	 * @param deleteSql
	 * @param params
	 * @return 成功返回true
	 */
	private boolean initDeleteConfigByPK(ReflectObject pojo, StringBuffer deleteSql, List<Object> params) {
		boolean ret = false;
		if (pojo == null || deleteSql == null || params == null) {
			return false;
		}
		String[] primaryKeyNames = pojo.getPrimaryKeyNames();
		if (primaryKeyNames == null || primaryKeyNames.length == 0) {
			return false;
		}
		deleteSql.append("delete from ").append(pojo.getTableName()).append(" where ");
		int paramsNum = 0;
		for (int i = 0; i < primaryKeyNames.length; i++) {
			String filedName = DaoSupportMapper.parseFieldName(primaryKeyNames[i]);
			if (filedName != null) {
				try {
					Field field = pojo.getClass().getDeclaredField(filedName);
					field.setAccessible(true);
					Object value = field.get(pojo);
					if (value != null) {
						if (params.size() != 0) {
							deleteSql.append(" and ");
						}
						deleteSql.append(primaryKeyNames[i]).append("=?");
						params.add(value);
						paramsNum++;
					}
				} catch (SecurityException e) {
					LogUtil.errorLog(MysqlResultSetParser.class, "SecurityException in this field:" + filedName, e);
					break;
				} catch (IllegalArgumentException e) {
					LogUtil.errorLog(MysqlResultSetParser.class, "IllegalArgumentException in this field:" + filedName, e);
					break;
				} catch (NoSuchFieldException e) {
					LogUtil.errorLog(MysqlResultSetParser.class, "NoSuchFieldException in this field:" + filedName, e);
					break;
				} catch (IllegalAccessException e) {
					LogUtil.errorLog(MysqlResultSetParser.class, "IllegalAccessException in this field:" + filedName, e);
					break;
				}
			}
		}
		if (paramsNum == primaryKeyNames.length) {
			ret = true;
		}
		return ret;
	}
	
	
	/**
	 * 初始化select count 的sql语句， 对pojo中的primary key值进行拼接。如果primary key是ID，也支持拼接
	 * 注意：pojo中的属性应该对象， 不能是int等基本类型
	 * @param selectSql
	 * @param params
	 * @return 成功返回true
	 */
	private boolean initSelectCountConfigByPK(ReflectObject pojo, StringBuffer selectSql, List<Object> params) {
		boolean ret = false;
		if (pojo == null || selectSql == null || params == null) {
			return false;
		}
		String[] primaryKeyNames = pojo.getPrimaryKeyNames();
		if (primaryKeyNames == null || primaryKeyNames.length == 0) {
			return false;
		}
		selectSql.append("select count(1) count from ").append(pojo.getTableName()).append(" where ");
		int paramsNum = 0;
		for (int i = 0; i < primaryKeyNames.length; i++) {
			String filedName = DaoSupportMapper.parseFieldName(primaryKeyNames[i]);
			if (filedName != null) {
				try {
					Field field = pojo.getClass().getDeclaredField(filedName);
					field.setAccessible(true);
					Object value = field.get(pojo);
					if (value != null) {
						if (params.size() != 0) {
							selectSql.append(" and ");
						}
						selectSql.append(primaryKeyNames[i]).append("=?");
						params.add(value);
						paramsNum++;
					} 
				} catch (SecurityException e) {
					LogUtil.errorLog(MysqlResultSetParser.class, "SecurityException in this field:" + filedName, e);
					break;
				} catch (IllegalArgumentException e) {
					LogUtil.errorLog(MysqlResultSetParser.class, "IllegalArgumentException in this field:" + filedName, e);
					break;
				} catch (NoSuchFieldException e) {
					LogUtil.errorLog(MysqlResultSetParser.class, "NoSuchFieldException in this field:" + filedName, e);
					break;
				} catch (IllegalAccessException e) {
					LogUtil.errorLog(MysqlResultSetParser.class, "IllegalAccessException in this field:" + filedName, e);
					break;
				}
			}
		}
		if (paramsNum == primaryKeyNames.length) {
			ret = true;
		}
		return ret;
	}
	
	/**
	 * 初始化select的sql语句， 对pojo中的primary key值进行拼接。如果primary key是ID，也支持拼接
	 * 注意：pojo中的属性应该对象， 不能是int等基本类型
	 * @param pojo
	 * @param selectSql
	 * @param params
	 * @return 成功返回true
	 */
	private boolean initSelectConfigByPK(ReflectObject pojo, StringBuffer selectSql, List<Object> params) {
		boolean ret = false;
		if (pojo == null || selectSql == null || params == null) {
			return false;
		}
		String[] primaryKeyNames = pojo.getPrimaryKeyNames();
		if (primaryKeyNames == null || primaryKeyNames.length == 0) {
			return false;
		}
		selectSql.append("select * from ").append(pojo.getTableName()).append(" where ");
		int paramsNum = 0;
		for (int i = 0; i < primaryKeyNames.length; i++) {
			String filedName = DaoSupportMapper.parseFieldName(primaryKeyNames[i]);
			if (filedName != null) {
				try {
					Field field = pojo.getClass().getDeclaredField(filedName);
					field.setAccessible(true);
					Object value = field.get(pojo);
					if (value != null) {
						if (params.size() != 0) {
							selectSql.append(" and ");
						}
						selectSql.append(primaryKeyNames[i]).append("=?");
						params.add(value);
						paramsNum++;
					}
				} catch (SecurityException e) {
					LogUtil.errorLog(MysqlResultSetParser.class, "SecurityException in this field:" + filedName, e);
					break;
				} catch (IllegalArgumentException e) {
					LogUtil.errorLog(MysqlResultSetParser.class, "IllegalArgumentException in this field:" + filedName, e);
					break;
				} catch (NoSuchFieldException e) {
					LogUtil.errorLog(MysqlResultSetParser.class, "NoSuchFieldException in this field:" + filedName, e);
					break;
				} catch (IllegalAccessException e) {
					LogUtil.errorLog(MysqlResultSetParser.class, "IllegalAccessException in this field:" + filedName, e);
					break;
				}
			}
		}
		if (paramsNum == primaryKeyNames.length) {
			ret = true;
		}
		return ret;
	}
	
	/**
	 * 初始化select的sql语句， 对pojo有赋值的属性进行and拼接， 如果表有ID字段，会忽略ID字段的拼接。
	 * 注意：pojo中的属性应该对象， 不能是int等基本类型
	 * @param pojo
	 * @param selectSql
	 * @param params
	 * @return 成功返回true
	 */
	public boolean initSelectConfigWithAnd(ReflectObject pojo, StringBuffer selectSql, List<Object> params) {
		boolean ret = false;
		if (pojo == null || selectSql == null || params == null) {
			return false;
		}
		selectSql.append("select * from ").append(pojo.getTableName()).append(" where ");
		List<TableFiled> tfList  = this.getObjectMetaData(pojo);
		if (tfList == null) {
			return false;
		}
		tfList = this.removeIdColumn(tfList); //过滤ID列字段
		int i;
		for (i = 0; i < tfList.size(); i++) {
			String label = tfList.get(i).columnName;
			String filedName = DaoSupportMapper.parseFieldName(label);
			try {
				Field field = pojo.getClass().getDeclaredField(filedName);
				field.setAccessible(true);
				Object value = field.get(pojo);
				if (value != null) {
					if (params.size() != 0) {
						selectSql.append(" and ");
					}
					selectSql.append(filedName).append("=?");
					params.add(value);
				}
				ret = true;
			} catch (SecurityException e) {
				LogUtil.errorLog(MysqlResultSetParser.class, "SecurityException in this field:" + filedName, e);
				return false;
			} catch (IllegalArgumentException e) {
				LogUtil.errorLog(MysqlResultSetParser.class, "IllegalArgumentException in this field:" + filedName, e);
				return false;
			} catch (NoSuchFieldException e) {
				LogUtil.errorLog(MysqlResultSetParser.class, "NoSuchFieldException in this field:" + filedName, e);
				return false;
			} catch (IllegalAccessException e) {
				LogUtil.errorLog(MysqlResultSetParser.class, "IllegalAccessException in this field:" + filedName, e);
				return false;
			}
		}
		if (ret && params.size() != 0) {
			ret = true;
		}
		return ret;
	}
	
	/**
	 * 初始化insert的sql语句， 对pojo有赋值的属性进行拼接， 如果表有ID字段，会忽略ID字段的拼接。
	 * 注意：pojo中的属性应该对象， 不能是int等基本类型
	 * @param pojo
	 * @param insertSql
	 * @param params
	 * @return 成功返回true
	 */
	public boolean initInsertConfig(ReflectObject pojo, StringBuffer insertSql, List<Object> params) {
		boolean ret = false;
		StringBuffer pStr = new StringBuffer();
		insertSql.append("insert into ").append(pojo.getTableName()).append("(");
		
		List<TableFiled> tfList  = this.getObjectMetaData(pojo);
		if (tfList == null) {
			return false;
		}
		tfList = this.removeIdColumn(tfList); //过滤ID列字段
		int i;
		for (i = 0; i < tfList.size(); i++) {
			String label = tfList.get(i).columnName;
			String filedName = DaoSupportMapper.parseFieldName(label);
			try {
				Field field = pojo.getClass().getDeclaredField(filedName);
				field.setAccessible(true);
				Object value = field.get(pojo);
				if (value != null) {
					if (params.size() != 0) {
						insertSql.append(",");
						pStr.append(",");
					}
					insertSql.append(label);
					pStr.append("?");
					params.add(value);
				}
			} catch (SecurityException e) {
				LogUtil.errorLog(MysqlResultSetParser.class, "SecurityException in this field:" + filedName, e);
				break;
			} catch (IllegalArgumentException e) {
				LogUtil.errorLog(MysqlResultSetParser.class, "IllegalArgumentException in this field:" + filedName, e);
				break;
			} catch (NoSuchFieldException e) {
				LogUtil.errorLog(MysqlResultSetParser.class, "NoSuchFieldException in this field:" + filedName, e);
				break;
			} catch (IllegalAccessException e) {
				LogUtil.errorLog(MysqlResultSetParser.class, "IllegalAccessException in this field:" + filedName, e);
				break;
			}
		}
		if (i == tfList.size()) {
			ret = true;
		}
		insertSql.append(") values(");
		insertSql.append(pStr.toString());
		insertSql.append(")");
		return ret;
	}

	/**
	 * 初始化update的sql语句， 对pojo有赋值的属性进行拼接， 
	 * 如果有ID字段且ID不为主键，会忽略ID字段的拼接。
	 * 注意：pojo中的属性应该对象， 不能是int等基本类型
	 * @param pojo
	 * @param updateSql
	 * @param params
	 * @return
	 */
	private boolean initUpdateConfig(ReflectObject pojo, StringBuffer updateSql, List<Object> params) {
		boolean ret = false;
		Object[] primaryKeyValues = new Object[pojo.getPrimaryKeyNames().length];
		updateSql.append("update ").append(pojo.getTableName()).append(" set ");
		
		List<TableFiled> tfList = this.getObjectMetaData(pojo);
		if (tfList == null) {
			return false;
		}
		if (!(pojo.getPrimaryKeyNames().length == 1 
				&& pojo.getPrimaryKeyNames()[0].toUpperCase().equals("ID"))) {
			tfList = this.removeIdColumn(tfList); //如果主键不为ID，则过滤ID
		}
		int i;
		for (i = 0; i < tfList.size(); i++) {
			String label = tfList.get(i).columnName;
			String filedName = DaoSupportMapper.parseFieldName(label);
			try {
				Field field = pojo.getClass().getDeclaredField(filedName);
				field.setAccessible(true);
				Object value = field.get(pojo);
				
				boolean isKey = false;
				if (primaryKeyValues.length != 0) {
					for (int j = 0; j < pojo.getPrimaryKeyNames().length; j++) {
						if (pojo.getPrimaryKeyNames()[j].toUpperCase().equals(label.toUpperCase())) {
							isKey = true;
							primaryKeyValues[j] = value;
							break;
						}
					}
				}
				if (isKey == false && value != null) {
					if (params.size() != 0) {
						updateSql.append(",");
					}
					updateSql.append(label).append(" =? ");
					params.add(value);
				}
			} catch (SecurityException e) {
				LogUtil.errorLog(MysqlResultSetParser.class, "SecurityException in this field:" + filedName, e);
				break;
			} catch (IllegalArgumentException e) {
				LogUtil.errorLog(MysqlResultSetParser.class, "IllegalArgumentException in this field:" + filedName, e);
				break;
			} catch (NoSuchFieldException e) {
				LogUtil.errorLog(MysqlResultSetParser.class, "NoSuchFieldException in this field:" + filedName, e);
				break;
			} catch (IllegalAccessException e) {
				LogUtil.errorLog(MysqlResultSetParser.class, "IllegalAccessException in this field:" + filedName, e);
				break;
			}
		}
		if (i == tfList.size()) {
			ret = true;
		}
		updateSql.append(" where ");
	    for(i = 0;i < pojo.getPrimaryKeyNames().length;i++){
	    	if (i != 0) {
	    		updateSql.append(" and ");
	    	}
	    	if(primaryKeyValues[i] != null) {
	    		updateSql.append(pojo.getPrimaryKeyNames()[i]);
	    		updateSql.append(" =? ");
	    		params.add(primaryKeyValues[i]);
	    	}
	    }
		return ret;
	}

	/**
	 * 获取表的字段，放入到list中
	 * @param obj
	 * @return 失败返回null
	 */
	public List<TableFiled> getObjectMetaData(ReflectObject obj) {
		String tableName = obj.getTableName();
		List<TableFiled> tsList = tableConf.get(tableName);
		if (tsList == null || tsList.size() == 0) {
			try {
				List<Object> tableFileds = super.query("show columns from " + tableName, new DaoSupportMapper(TableFiled.class));
				tsList = new ArrayList<TableFiled>();
				for (int i = 0; i < tableFileds.size(); i++) {
					tsList.add((TableFiled) tableFileds.get(i));
				}
			} catch(DataAccessException e) {
				LogUtil.debugLog(e.getMessage());
				return null;
			}
			tableConf.put(tableName, tsList);
		}
		return tsList;
	}
	
	/**
	 * @param list
	 * @return
	 */
	private List<TableFiled> removeIdColumn(List<TableFiled> list) {
		List<TableFiled> newlist = new ArrayList<BaseJdbcTemplate.TableFiled>(list);
		if (newlist != null) {
			for (int i = 0; i < newlist.size(); i++) {
				TableFiled tmpFiled = newlist.get(i);
				if (tmpFiled.columnName.equals("ID")) {
					newlist.remove(i);
					break;
				}
			}
		}
		return newlist;
	}

	public static class TableFiled extends ReflectObject {
		/**
		 * @Fields serialVersionUID : TODO
		*/
		private static final long serialVersionUID = 1L;
		String columnName;
	}
}
